#!/bin/bash

yum -y install bash-completion

yum -y install wget

# 设置MariaDB的yum源
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

# 设置Percona的yum源
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm -y

# 设置epel源
yum install http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y

# 刷新本地yum源的缓存
yum clean all
yum make cache

# 安装libev库,被xtrabackup所依赖
yum -y install libev.x86_64.0.4.15-3.el7

# 安装maxscale,实现读写分离
yum install maxscale -y

# 卸载7系中,系统自带的防火墙
yum -y remove firewalld

# 安装iptables防火墙
yum -y install iptables-services

# 卸载mysql相关的库,为安装其他累mysql系统做准备
yum remove mysql* -y

# 开始安装percona数据库
yum install Percona-Server-server-57 -y
yum install Percona-Server-devel-57-5.7.21-20.1.el7.x86_64 -y

# 开始安装PMM客户端
yum install pmm-client -y

# 开始安装percona工具包
yum install percona-toolkit -y

# 开始安装percona备份工具
yum install percona-xtrabackup -y

# 安装maxscale
yum install maxscale -y

# 安装jemalloc库,提高高并发场景的性能
yum -y install jemalloc

# 开启jemalloc的支持
echo 'LD_PRELOAD=/usr/lib64/libjemalloc.so.1' >> /etc/sysconfig/mysql

# 开启IP转发
echo 'net.ipv4.ip_forward = 1' >> /usr/lib/sysctl.d/00-system.conf

# 设置swap调度
echo 'vm.swappiness = 5' >> /usr/lib/sysctl.d/00-system.conf

# 设置内存脏页占比
echo 'vm.dirty_ratio = 5' >> /usr/lib/sysctl.d/00-system.conf

# 设置后台刷脏页的阈值比例
echo 'vm.dirty_background_ratio = 10' >> /usr/lib/sysctl.d/00-system.conf

# 刷新系统内核变量的值为当前设定值
sysctl -p

# 调整系统内核限定
echo '*               soft    nofile          65535' >> /etc/security/limits.conf
echo '*               hard    nofile          65535' >> /etc/security/limits.conf
echo '*               soft    nproc           65535' >> /etc/security/limits.conf
echo '*               hard    nproc           65535' >> /etc/security/limits.conf

# 使得iptables开机自动启动
systemctl enable iptables

# 启动iptables
systemctl start iptables

# 使得percona开机启动
systemctl enable mysqld

# 启动percona
systemctl start mysqld

# 获取percona数据库的初始连接密码
cat /var/log/mysqld.log | grep password
cat /var/log/mysqld.log | grep password|awk '{print $11}'

# 参考下面执行初始化数据库
# mysql -uroot -p`cat /var/log/mysqld.log | grep password|awk '{print $11}'` -e"alter user 'root'@'localhost' identified by 'Woshisui123\!\@\#'"
# mysql -uroot -pWoshisui123\!\@\# -e"uninstall plugin validate_password"
# mysql -uroot -pWoshisui123\!\@\# -e"grant all privileges on *.* to 'root'@'%' identified by 'xjjy1981123!@#' with grant option"
